//
//  IGSearchBar.m
//  jingdongfang
//
//  Created by BOE on 2017/8/24.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGSearchBar.h"

@implementation IGSearchBar

+ (IGSearchBar *)searchBarSearchIconBlock:(SearchIconBlock)searchIconBlock {
    IGSearchBar *searchBar = [[IGSearchBar alloc] init];
    searchBar.searchIconBlock = searchIconBlock;
    searchBar.showsCancelButton = YES;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
    //修改标题和标题颜色
    [cancleBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    for (UIView *view0 in searchBar.subviews) {
        for (UIView *view1 in view0.subviews) {
            
            if ([NSStringFromClass([view1 class]) hasPrefix:@"UI"] && [NSStringFromClass([view1 class]) hasSuffix:@"SearchBarTextField"]) {
                UITextField *textField = (UITextField *)view1;
                UIImageView *imgView = (UIImageView *)textField.leftView;
                imgView.image = [UIImage imageNamed:@"circle_search_down"];
                imgView.userInteractionEnabled = YES;
                __weak typeof(imgView) weakimgView = imgView;
                [imgView setTapActionWithBlock:^{
                    if (searchBar.searchIconBlock) {
                        searchBar.searchIconBlock(weakimgView);
                    }
                }];
            }
        }
    }
    
    return searchBar;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
